﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebChamCong
{
    public class LabelGridView : ITemplate
    {
        public static void ConfirmMessageBox(ref System.Web.UI.WebControls.WebControl webControl, string message, string webControlName, string eventName)
        {

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("var str = trim(document.forms[0].{0}.value);", webControlName);
            sb.Append("if(str != null)");
            sb.Append("{");
            sb.Append("if(str != '')");
            sb.Append("{");
            sb.AppendFormat("var bRet = confirm('{0}');", message);
            sb.Append("if(!bRet)");
            sb.AppendFormat("document.forms[0].{0}.value = '';", webControlName);
            sb.Append("return bRet;");
            sb.Append("}");
            sb.Append("}");

            webControl.Attributes.Add(eventName, sb.ToString());

        }

        private DataControlRowType templateType;
        private string columnName;
        private string columnNameBinding;
        private string controlType;
        private DateTime localTime;

        public LabelGridView(DataControlRowType type, string colname, string colNameBinding, string ctlType, DateTime time)
        {
            templateType = type;
            columnName = colname;
            columnNameBinding = colNameBinding;
            controlType = ctlType;
            localTime = time;
        }

        public void InstantiateIn(Control container)
        {
            switch (templateType)
            {         
                case DataControlRowType.DataRow:
                    if (controlType == "Label")
                    {
                        LinkButton lb = new LinkButton();                      
                        lb.DataBinding += new EventHandler(this.ctl_OnDataBinding);
                        lb.CommandName = "choose_shift";                        
                        container.Controls.Add(lb);
                    }
                    break;
                default:
                    break;
            }

        }

        public void ctl_OnDataBinding(object sender, EventArgs e)
        {
            if (sender.GetType().Name == "LinkButton")
            {
                LinkButton lb = (LinkButton)sender;
                GridViewRow container = (GridViewRow)lb.NamingContainer;
                DataRowView dr = (DataRowView)container.DataItem;
                string value  = DataBinder.Eval(dr, columnName.ToString()) as string;
                string commandAgru = DataBinder.Eval(dr, "ID") as string;
                commandAgru += "," + columnName.ToString();
                commandAgru += "," + this.localTime.Month;
                commandAgru += "," + this.localTime.Year;
                lb.CommandArgument = commandAgru;
                lb.Text = BUS.LichBUS.GetShiftCodeByID(Int32.Parse(value));
            }           
        }
    }
}